﻿@inherits ClientSectionCompontentBase

<MRow Class="pt-4 mr-1">
    <MCol Md=12>
        <SGroupBox Title="@T("GrantType")" Style="display: flex;flex-wrap: wrap;">
            @foreach (var grantType in Dto.GrantTypes)
            {
                <MCheckbox @bind-Value="@grantType.Selected" Label="@grantType.DisplayValue" TValue="bool" Style="width:340px;"/>
            }
        </SGroupBox>
    </MCol>
    <MCol Md=12>
        <div class="block-center">
            <STextField @bind-Value="Dto.RedirectUri" Label="@T("CallBackRedirectUri")" Suffix="@redirectUriSuffix" />
            <SButton Icon OnClick="AddCallBackRedirectUri" Color="regular3">
                <SIcon>@IconConstants.Add</SIcon>
            </SButton>
        </div>
    </MCol>
    @foreach (var uri in Dto.RedirectUris)
    {
        <MCol Md=6 Class="block-center">
            <STextField HideDetails="true" Value="uri" Filled Disabled Class="flex-grow-1" />
            <SButton Icon OnClick="()=>RemoveCallBackRedirectUri(uri)" Color="regular3">
                <SIcon Tooltip="@T("Delete")">mdi-close-circle</SIcon>
            </SButton>
        </MCol>
    }
    <MCol Md=12>
        <div class="block-center">
            <STextField @bind-Value="Dto.PostLogoutRedirectUri" Label="@T("PostLogoutRedirectUri")" Suffix="@logoutRedirectUriSuffix"/>
            <SButton Icon OnClick="AddLogoutRedirectUri" Color="regular3">
                <SIcon>@IconConstants.Add</SIcon>
            </SButton>
        </div>
    </MCol>
    @foreach (var uri in Dto.PostLogoutRedirectUris)
    {
        <MCol Md=6 Class="block-center">
            <STextField HideDetails="true" Value="uri" Filled Disabled />
            <MButton Icon OnClick="()=>RemoveLogoutRedirectUri(uri)" Color="regular3">
                <SIcon Tooltip="@T("Delete")">mdi-close-circle</SIcon>
            </MButton>
        </MCol>
    }
    <MCol Md=6>
        <STextField @bind-Value="Dto.FrontChannelLogoutUri" Label="@T("FrontChannelLogoutUri")" Tooltip="@T("FrontChannelLogoutUriTips")" />
    </MCol>
    <MCol Md=6>
        <SCheckbox @bind-Value="Dto.FrontChannelLogoutSessionRequired" Label="@T("FrontChannelLogoutSessionRequired")" Tooltip="@T("FrontChannelLogoutSessionRequiredTips")" />
    </MCol>
    <MCol Md=6>
        <STextField @bind-Value="Dto.BackChannelLogoutUri" Label="@T("BackChannelLogoutUri")" Tooltip="@T("BackChannelLogoutUriTips")" />
    </MCol>
    <MCol Md=6>
        <SCheckbox @bind-Value="Dto.BackChannelLogoutSessionRequired" Label="@T("BackChannelLogoutSessionRequired")" Tooltip="@T("FrontChannelLogoutSessionRequiredTips")" />
    </MCol>
    <MCol Md=6>
        <SNumberTextField @bind-Value="Dto.UserSsoLifetime" Label="@T("UserSsoLifetime")" Tooltip="@T("UserSsoLifetimeTips")" />
    </MCol>
    <MCol Md=6>
        <SCheckbox @bind-Value="Dto.EnableLocalLogin" Label="@T("EnableLocalLogin")" Tooltip="@T("EnableLocalLoginTips")" />
    </MCol>
    @*        <MCol Md=12>
    <STextField TValue="string" Label="IdentityProviderRestriction" />
    </MCol>*@
</MRow>

@code {

    [Parameter]
    public ClientAuthenticationDto Dto { get; set; } = new();

    [Parameter]
    public EventCallback<ClientAuthenticationDto> DtoChanged { get; set; }

    readonly string redirectUriSuffix = "/signin-oidc";
    readonly string logoutRedirectUriSuffix = "/signout-callback-oidc";

    private void AddCallBackRedirectUri()
    {
        if (!Uri.TryCreate(Dto.RedirectUri, UriKind.Absolute, out var uri))
        {
            PopupService.EnqueueSnackbarAsync(T("Is not a valid URL address"), AlertTypes.Error);
            return;
        }
        var redirectUri = $"{Dto.RedirectUri.TrimEnd('/')}{redirectUriSuffix}".ToLower();
        if (Dto.RedirectUris.Contains(redirectUri))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate item"), AlertTypes.Error);
            return;
        }
        Dto.RedirectUris.Add(redirectUri);
        Dto.RedirectUri = string.Empty;
    }

    private void AddLogoutRedirectUri()
    {
        if (!Uri.TryCreate(Dto.PostLogoutRedirectUri, UriKind.Absolute, out var uri))
        {
            PopupService.EnqueueSnackbarAsync(T("Is not a valid URL address"), AlertTypes.Error);
            return;
        }
        var postLogoutRedirectUri = $"{Dto.PostLogoutRedirectUri.TrimEnd('/')}{logoutRedirectUriSuffix}";
        if (Dto.PostLogoutRedirectUris.Contains(postLogoutRedirectUri))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate item"), AlertTypes.Error);
            return;
        }
        Dto.PostLogoutRedirectUris.Add(postLogoutRedirectUri);
        Dto.PostLogoutRedirectUri = string.Empty;
    }

    private void RemoveCallBackRedirectUri(string val)
    {
        Dto.RedirectUris.Remove(val);
    }

    private void RemoveLogoutRedirectUri(string val)
    {
        Dto.PostLogoutRedirectUris.Remove(val);
    }
}
